$(function () {
    $('#save-account-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $('#banner-dialog');
        var pay_card = $("input[name='pay_card']").val();
        var bank_name = $("input[name='bank_name']").val();
        var real_name = $("input[name='real_name']").val();
        // var address = $("input[name='address']").val();
        var pay_img = $("input[name='pay_img']").val();
        var pay_name = $("input[name='pay_name']").val();
        var submitType = self.attr("data-type");
        var account_id = self.attr('data-id');

        var url = '';
        if (submitType == 'update'){
            url = '/cms/u_pay_account/';
        }else{
            url = '/cms/add_pay_account/';
        }

        zlajax.post({
            'url':url,
            'data':{
                'pay_card':pay_card,
                'account_id':account_id,
                'bank_name':bank_name,
                'pay_img':pay_img,
                'real_name':real_name,
                'pay_name':pay_name,
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code']==200){
                    window.location.reload()
                }else{
                    zlalert.alertInfo(data['message']);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
})


$(function () {
    $(".update-account-btn").click(function (event) {
        var self = $(this);
        var dialog = $('#account-dialog');
        dialog.modal('show');

        var tr = self.parent().parent();
        var pay_card = tr.attr("data-card");
        var bank_name = tr.attr("data-name");
        var real_name = tr.attr("data-real");
        var pay_img = tr.attr("data-img");
        var pay_name = tr.attr("data-pay_name");

        var cardInput = dialog.find("input[name='pay_card']");
        var nameInput = dialog.find("input[name='bank_name']");
        var realInput = dialog.find("input[name='real_name']");
        var payInput = dialog.find("input[name='pay_img']");
        var pay_nameInput = dialog.find("input[name='pay_name']");
        var savebtn = dialog.find("#save-account-btn");

        if (bank_name == 'None'){
            bank_name = '无'
        }
         if (pay_img == 'None'){
            pay_img = '无'
        }


        cardInput.val(pay_card);
        nameInput.val(bank_name);
        realInput.val(real_name);
        payInput.val(pay_img);
        pay_nameInput.val(pay_name);

        savebtn.attr("data-type",'update');
        savebtn.attr("data-id",tr.attr('data-id'));
    })
});


$(function () {
    $('.delete-pay-btn').click(function (event) {
        var self = $(this)
        var tr = self.parent().parent();
        var pay_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':'您确定要删除这个产品吗？',
            'confirmCallback':function () {
                zlajax.post({
                    'url':'/cms/del_pay_account/',
                    'data':{
                        'pay_id':pay_id
                    },
                    'success':function (data) {
                        if(data['code']==200){
                            window.location.reload()
                        }else{
                            zlalert.alertError(data['message'])
                        }
                    }
                })
            }
        })
    })
});